package com.hzb.shop.autocode.xml;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.hzb.shop.autocode.file.FileUtils;
import com.hzb.shop.autocode.sql.CommonUtils;
import com.hzb.shop.autocode.sql.TableColumn;
import com.hzb.shop.autocode.sql.TableSchme;

public class Builder {

	public static void builder(String daoPackPreff,String entiyPackPreff,String tableName){
		String url = "jdbc:mysql://192.168.45.129:3306/hzb.shop?characterEncoding=utf-8";
		String user = "root";
		String pwd = "houzhangbo";
		String templateFile = Builder.class.getClassLoader().getResource("xml.template/template.xml").getFile();
		List<TableColumn> list = TableSchme.getColumns(url,user,pwd,tableName);
		List<String> xmlList = FileUtils.read(templateFile);
		List<String> xmlList1 = new ArrayList<String>();
		String $1 = daoPackPreff+getClassName(tableName)+"Mapper";
		String $2 = entiyPackPreff+getClassName(tableName);
		String $3 = getResultList(list);
		String $4 = tableName;
		String $5 = getSqlInsert(list);
		String $6 = getSqlInsertVal(list);
		String $7 = getQueryParam(list,0);
		String $8 = getQueryParam(list,1);
		for(String xml:xmlList){
			xml = xml.replaceAll("dom1", $1);
			xml = xml.replaceAll("dom2", $2);
			xml = xml.replaceAll("dom3", $3);
			xml = xml.replaceAll("dom4", $4);
			xml = xml.replaceAll("dom5", $5);
			xml = xml.replaceAll("dom6", $6);
			xml = xml.replaceAll("dom7", $7);
			xml = xml.replaceAll("dom8", $8);
			xmlList1.add(xml);
		}
		tableName = tableName.replaceAll("_", "");
		FileUtils.write(xmlList1, "d:/xml/"+tableName+".xml");
	}
	public static String getClassName(String tableName){
		String str = "";
		if(tableName.indexOf("_")>-1){
			str = doJdbcCloToJava(tableName);
		}else{
			str = tableName;
		}
		return str.substring(0, 1).toUpperCase()+str.substring(1,str.length());
	}
	/**
	 * 获取insert column
	 * @param list
	 * @return
	 */
	public static String getSqlInsert(List<TableColumn> list){
		StringBuilder sb = new StringBuilder("");
		for(int i=1;i<list.size();i++){
			if(i==list.size()-1){
				sb.append(list.get(i).getName());
			}else{
				sb.append(list.get(i).getName()).append(",");
			}
		}
		return sb.toString();
	}
	/**
	 * 获取insert values
	 * @param list
	 * @return
	 */
	public static String getSqlInsertVal(List<TableColumn> list){
		StringBuilder sb = new StringBuilder("");
		for(int i=1;i<list.size();i++){
			if(i==list.size()-1){
				sb.append("#{"+jdbcCloToJava(list.get(i).getName())+"}");
			}else{
				sb.append("#{"+jdbcCloToJava(list.get(i).getName())).append("},");
			}
		}
		return sb.toString();
	}
	/**
	 * jdbc 转换为java类型
	 * @param name
	 * @return
	 */
	public static String jdbcCloToJava(String name){
		if(name.indexOf("_")>-1){
			return doJdbcCloToJava(name);
		}else{
			return name;
		}
	}
	public static String doJdbcCloToJava(String s){
		while(s.indexOf("_")>-1){
			s = s.substring(0,s.indexOf("_")+1)+s.substring(s.indexOf("_")+1,s.indexOf("_")+2).toUpperCase()+s.substring(s.indexOf("_")+2,s.length());
			s = s.substring(0,s.indexOf("_"))+s.substring(s.indexOf("_")+1,s.length());
		}
		return s;
	}
	static String[] arr = new String[]{"id","create_time","update_time"};
	/**
	 * resultmap 列表
	 * @param list
	 * @return
	 */
	public static String getResultList(List<TableColumn> list){
		StringBuilder sb = new StringBuilder("");
		for(TableColumn c:list){
			int i=0;
			for(String s:arr){
				if(c.getName().equals(s)){
					i++;
				}
			}
			if(i==0){
				sb.append(Character.Tabulation.getName()).append(Character.Tabulation.getName()).append("<result column=\"").append(c.getName()).
				append("\" property=\"").append(jdbcCloToJava(c.getName())).append("\" jdbcType=\"");
				if("DATETIME".equals(c.getTypeName())){
					sb.append("DATE\" javaType=\"java.util.Date\" />");
				}else if("INT".equals(c.getTypeName())){
					sb.append("TINYINT\" />");
				}else{
					sb.append(c.getTypeName()).append("\" />");
				}
				sb.append(Character.Line.getName());
				System.out.println("private "+CommonUtils.JdbcToJavaType(c.getTypeName())+" "+jdbcCloToJava(c.getName())+";");
			}
		}
		return removeTabAndLine(sb);
	}
	/**
	 * update和select的参数
	 * @param list
	 * @param type
	 * @return
	 */
	public static String getQueryParam(List<TableColumn> list,int type){
		StringBuilder sb = new StringBuilder("");
		for(TableColumn t:list){
			doQueryParam(t,sb,type);
		}
		return removeTabAndLine(sb);
	}
	public static void doQueryParam(TableColumn t,StringBuilder sb,int type){
		sb.append(Character.Tabulation.getName()).append(Character.Tabulation.getName()).append("<if test=\"").append(jdbcCloToJava(t.getName())).append("!=null\">").append(Character.Line.getName());
		if(0==type){
			sb.append(Character.Tabulation.getName()).append(Character.Tabulation.getName()).append(Character.Tabulation.getName()).append(",");
		}else if(1==type){
			sb.append(Character.Tabulation.getName()).append(Character.Tabulation.getName()).append(" AND ");
		}
		sb.append(t.getName()).append("=#{").append(jdbcCloToJava(t.getName())).append("}").append(Character.Line.getName());
		sb.append(Character.Tabulation.getName()).append(Character.Tabulation.getName()).append("</if>").append(Character.Line.getName());
	}
	/**
	 * 移除结尾多余的换行符
	 * @param sb
	 * @return
	 */
	public static String removeLine(StringBuilder sb){
		if(sb.toString().endsWith(Character.Line.getName())){
			return sb.toString().substring(0, sb.toString().lastIndexOf(Character.Line.getName()));
		}else{
			return sb.toString();
		}
	}
	public static String removeTabAndLine(StringBuilder sb){
		String str = sb.toString();
		while(str.startsWith(Character.Tabulation.getName())){
			str = str.substring(str.indexOf(Character.Tabulation.getName())+Character.Tabulation.getName().length(),str.length());
		}
		if(str.endsWith(Character.Line.getName())){
			return str.substring(0, str.lastIndexOf(Character.Line.getName()));
		}else{
			return str;
		}
	}
	enum Character{
		Tabulation(0,"\t"),
		Line(1,"\n");
		private int code;
		private String name;
		Character(int code,String name){
			this.code = code;
			this.name = name;
		}
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
	}
	
	public static void main(String[] args) {
		builder("com.hzb.shop.goods.mapper.","com.hzb.shop.goods.entiy.","shop_goods_stock");
	}
}
